<?php 
/*
 module:		活动列表
 create_time:	2022-03-25 19:52:07
 author:		
 contact:		
*/

namespace app\backend\controller;

use app\backend\service\ZhActivityService;
use app\backend\model\ZhActivity as ZhActivityModel;
use think\facade\Db;

class ZhActivity extends Admin {


	/*首页数据列表*/
	function index(){
		if (!$this->request->isAjax()){
			return view('index');
		}else{
			$limit  = $this->request->post('limit', 20, 'intval');
			$offset = $this->request->post('offset', 0, 'intval');
			$page   = floor($offset / $limit) +1 ;

			$where = [];
			$where['title'] = ['like',$this->request->param('title', '', 'serach_in')];

			$start_time_start = $this->request->param('start_time_start', '', 'serach_in');
			$start_time_end = $this->request->param('start_time_end', '', 'serach_in');

			$where['start_time'] = ['between',[strtotime($start_time_start),strtotime($start_time_end)]];

			$sign_up_start_time_start = $this->request->param('sign_up_start_time_start', '', 'serach_in');
			$sign_up_start_time_end = $this->request->param('sign_up_start_time_end', '', 'serach_in');

			$where['sign_up_start_time'] = ['between',[strtotime($sign_up_start_time_start),strtotime($sign_up_start_time_end)]];
			$where['is_charge'] = $this->request->param('is_charge', '', 'serach_in');
			$where['status'] = $this->request->param('status', '', 'serach_in');

			$sign_up_end_time_start = $this->request->param('sign_up_end_time_start', '', 'serach_in');
			$sign_up_end_time_end = $this->request->param('sign_up_end_time_end', '', 'serach_in');

			$where['sign_up_end_time'] = ['between',[strtotime($sign_up_end_time_start),strtotime($sign_up_end_time_end)]];
            $where['wxapp_id'] = session('backend.wxapp_id');
			$order  = $this->request->post('order', '', 'serach_in');	//排序字段 bootstrap-table 传入
			$sort  = $this->request->post('sort', '', 'serach_in');		//排序方式 desc 或 asc

			$field = 'activity_id,title,start_time,sign_up_start_time,is_charge,status,sign_up_end_time';
			$orderby = ($sort && $order) ? $sort.' '.$order : 'activity_id desc';

			$res = ZhActivityService::indexList(formatWhere($where),$field,$orderby,$limit,$page);
			return json($res);
		}
	}

	/*修改排序开关按钮操作*/
	function updateExt(){
		$postField = 'activity_id,status';
		$data = $this->request->only(explode(',',$postField),'post',null);
		if(!$data['activity_id']) $this->error('参数错误');
		try{
			ZhActivityModel::update($data);
		}catch(\Exception $e){
			abort(config('my.error_log_code'),$e->getMessage());
		}
		return json(['status'=>'00','msg'=>'操作成功']);
	}

	/*添加*/
	function add(){
		if (!$this->request->isPost()){
			return view('add');
		}else{
			$postField = 'title,subtitle,start_time,sign_up_start_time,address,coordinate,content,images,is_charge,sign_up_price,wxapp_id,status,createtime,activity_num,sign_up_end_time';
			$data = $this->request->only(explode(',',$postField),'post',null);
			$res = ZhActivityService::add($data);
			return json(['status'=>'00','msg'=>'添加成功']);
		}
	}

	/*修改*/
	function update(){
		if (!$this->request->isPost()){
			$activity_id = $this->request->get('activity_id','','serach_in');
			if(!$activity_id) $this->error('参数错误');
			$this->view->assign('info',checkData(ZhActivityModel::find($activity_id)));
			return view('update');
		}else{
			$postField = 'activity_id,title,subtitle,start_time,sign_up_start_time,address,coordinate,content,images,is_charge,sign_up_price,wxapp_id,status,createtime,activity_num,sign_up_end_time';
			$data = $this->request->only(explode(',',$postField),'post',null);
			$res = ZhActivityService::update($data);
			return json(['status'=>'00','msg'=>'修改成功']);
		}
	}

	/*删除*/
	function delete(){
		$idx =  $this->request->post('activity_id', '', 'serach_in');
		if(!$idx) $this->error('参数错误');
		try{
			ZhActivityModel::destroy(['activity_id'=>explode(',',$idx)],true);
		}catch(\Exception $e){
			abort(config('my.error_log_code'),$e->getMessage());
		}
		return json(['status'=>'00','msg'=>'操作成功']);
	}

	/*查看详情*/
	function view(){
		$activity_id = $this->request->get('activity_id','','serach_in');
		if(!$activity_id) $this->error('参数错误');
		$this->view->assign('info',ZhActivityModel::find($activity_id));
		return view('view');
	}



}

